
package com.sfdlJPush.controller;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sfdlJPush.R;
import com.sfdlJPush.api.LoginRequest;
import com.sfdlJPush.api.LoginResponse;
import com.sfdlJPush.controller.api.ApiListenrDefaultImpl;
import com.sfdlJPush.controller.api.B5MBaseResponse;
import com.sfdlJPush.model.User;
import com.sfdlJPush.utils.B5MActivityHelper;
import com.sfdlJPush.utils.B5MBaseActivity;
import com.sfdlJPush.utils.B5MPreferenceHelper;
import com.sfdlJPush.utils.B5MPreferenceKeys;
import com.sfdlJPush.utils.B5MToaster;
import com.sfdlJPush.utils.EventBus.SubscriberChangeListener;
import com.sfdlJPush.utils.UserInfoManage;

public class LoginActivity extends B5MBaseActivity implements SubscriberChangeListener
{
    private EditText et_username, et_password;
    private LoginRequest mLoginRequest;
    private ProgressBar mProgressBar;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.login;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        findViewById(R.id.bt_main_left).setVisibility(View.GONE);
        findViewById(R.id.bt_main_right).setVisibility(View.GONE);
        ((TextView) findViewById(R.id.tv_main_title)).setText("LOGIN");
        mProgressBar = (ProgressBar) findViewById(R.id.pb_head);
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mLoginRequest = new LoginRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                LoginResponse loginResponse = (LoginResponse) b5mBaseResponse;
                User user = loginResponse.user;
                user.username = mLoginRequest.username;
                user.password = mLoginRequest.password;
                B5MPreferenceHelper.saveStringValue(mContext, B5MPreferenceKeys.USERNAME, mLoginRequest.username);
                B5MPreferenceHelper.saveStringValue(mContext, B5MPreferenceKeys.PASSWORD, mLoginRequest.password);
                UserInfoManage.getInstance().saveUser(new Gson().toJson(user));
                finish();
                B5MActivityHelper.startActivity(mActivity, MainActivity.class);
            }
        }, mProgressBar);
        String username = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USERNAME);
        String password = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.PASSWORD);
        if (!TextUtils.isEmpty(username))
        {
            et_username.setText(username);
        }
        if (!TextUtils.isEmpty(password))
        {
            et_password.setText(password);
        }
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    public void submit(View view)
    {
        String username = et_username.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(username))
        {
            B5MToaster.showShort(mActivity, "Username cannot be empty", 0);
            return;
        }
        if (TextUtils.isEmpty(password))
        {
            B5MToaster.showShort(mActivity, "Password cannot be empty", 0);
            return;
        }
        mLoginRequest.username = username;
        mLoginRequest.password = password;
        mLoginRequest.start();
    }

    public void toRegister(View view)
    {
        //        B5MActivityHelper.startActivity(mActivity, RegisterActivity.class);
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        //        EventBus.getInstance().subscribe(EventNames.REGISTER_SUCCESS, this);
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        //        EventBus.getInstance().unSubscribe(EventNames.REGISTER_SUCCESS, this);
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName , Object notificateContent)
    {
        // TODO Auto-generated method stub
        //        if (EventNames.REGISTER_SUCCESS.equals(notificationName))
        //        {
        //            if (notificateContent != null)
        //            {
        //                User user = (User) notificateContent;
        //                et_username.setText(user.username);
        //                et_password.setText(user.password);
        //            }
        //        }
    }

    public void toGetPassword(View view)
    {
        //        B5MActivityHelper.startActivity(mActivity, GetPassword.class);
    }
}
